<?php 
defined('ACC')||exit('ACC Denied');
 //goods 表繼承Model類
//負責添加商品到數據庫的Model
class goods_Model extends Model{
	protected $table='goods';
	protected $primarykey='goods_id';
	protected $file=array('goods_sn','cat_id','brand_id','goods_name','shop_price','market_price','goods_number',
'click_count','goods_weight','goods_brief','goods_desc','thumb_img','goods_img','ori_img','is_on_sale','is_delete','is_best',
'is_new','is_hot','add_time','last_update');
	protected $autofill=array(array('is_new',0),array('is_hot','default',0),array('add_time','func','time'));
	protected $_valid=array(
		array('goods_name',1,'必须有商品名','require'),
		array('email',1,'邮箱不能为空','require'),
		array('cat_id',1,'栏目id必须是整形值','number'),
		array('is_new',0,'is_new必须是0或者1','in','0,1'),
		array('goods_brief',2,'商品简介在10到100字符之间','length','1,10'),
		array('email',2,'email不合法','email')
		);
	public function trash($id){
		return $this->update(array('is_delete'=>1),$id);
	}
	/**
	 * @return array
     */
	public function getGoods(){
		$sql="select * from goods where is_delete=0";
		return $this->db->getAll($sql);
	}
	public function getTrash(){
		$sql="select * from goods where is_delete=1";
		return $this->db->getAll($sql);
	}
	public function createSn(){
		$sn='EC'.date('Ymd').mt_rand(10000,99999);
		$sql='select * from '.$this->table." where goods_sn='".$sn."'";
		return $this->db->getOne($sql)?$this->createSn():$sn;
		
	}
/**	*****************未检测的方法************************************************
*index,php取 cat_id大栏目商品用
*取出指定栏目的商品
*$cat_id=$_GET['cat_id'];
*$sql=select ..from goods where cat_id=$cat_id;
*这是错的；因为$cat_id对应的栏目可能是个大栏目，而大栏目下没有商品
*商品放在大栏目下的小栏目下面
*因此，正确的做法，是找出$cat_id的所有子孙栏目
*然后再查所有的$cat_id及其子孙栏目的下的商品
*/
	public function catGoods($cat_id){
		$category=new Categorymodel();
		$cats=$category->select();//取出栏目下所有商品
		$sons=$category->getCatTree($cat,$cat_id);//取出指定栏目下的子孙栏目
		$sub =array($cat_id);
		if (!empty($sons)) {//没有子孙栏目
			foreach ($sons as $v) {
				$sub[]=$v['cat_id'];
			}
		}
		$in=implode(',',$sub);
		$sql="select goods_id,goods_name,shop_price,market_price,thumb_img from ".$this->table." where cat_id in(".$in.") order by add_time limit 5";
		return $this->db->getAll($sql);
	}
/***************************************未在index.php实现
/**前台取出指定条目新品，is_new=1，按add_time排序
*@param int $n 取出的新品数量
*@return arr 指定数量新品的数组
*/
	public function getNew($n){
		$sql="select goods_id,goods_name,shop_price,market_price,thumb_img from ".$this->table." order by add_time limit 5";
		return $this->db->getAll();
	}
	public function find($id){
	$sql="select * from ".$this->table." where ".$this->primarykey."=".$id;
	return $this->db->getAll();
    }
    /**
     * 获取购物车商品中的详细信息，注意不能把$items 中的商品数量丢了
     * @param $items 购物车中的商品数组
     * @return array 商品里面的详细信息
     */
    public function getCartGoods($items){
        foreach ($items as $k=>$v){
            $sql="select goods_id,goods_name,thumb_img,shop_price,market_price from ".$this->table.
                " where goods_id=".$k;
            $row=$this->db->getRow($sql);
            $items[$k]['thumb_img']=$row['thumb_img'];
            $items[$k]['market_price']=$row['market_price'];
        }
        return $items;
    }
}